package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.ArticleSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * @Description：文章搜索业务逻辑处理层
 * @Author: liulei
 * @Date: 2019年10月27日
 * @Version: V1.0
 **/
@Service
public class ArticleSearchService {

    @Autowired
    private ArticleSearchDao articleSearchDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 文章搜索，添加文章
     * @param articleSearch
     */
    public void save(ArticleSearch articleSearch){
        articleSearch.setId(idWorker.nextId()+"");
        articleSearchDao.save(articleSearch);
    }

    /**
     * 根据关键字模糊搜索+分页
     * @param key
     * @param size
     * @param page
     * @return
     */
    public Page<ArticleSearch> findByKey(String keyword, int size, int page) {
        Pageable pageable = PageRequest.of(page-1, size);
        return articleSearchDao.findByTitleOrContentLike(keyword, keyword, pageable);
    }
}
